package com.jic.market.impl;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.entity.Material;
import com.jic.market.mapper.MaterialMapper;
import com.jic.market.request.*;
import com.jic.market.service.MaterialService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 素材表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-02-27 04:39:32
 */
@Service
public class MaterialServiceImpl extends MarketBaseService implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    @Override
    public RestResult<PageResult<Material>> queryPage(MaterialQueryRequest request) {
        return executeQuery(() -> {
            AdminOperate operate = request.getAdminOperate();
            //计算下标
            int startIndex = (request.getStart() - 1) * request.getLimit();
            Material material = new Material();
            material.setCategoryId(request.getCategoryId());
            material.setMaterialName(request.getMaterialName());
            material.setIsDelete(false);
            if (null != operate) {
                material.setStoreId(operate.getOperatorStoreId());
                material.setMerchantId(operate.getOperatorMerchantId());
                material.setDeptId(operate.getOperatorDeptId());
            }
            List<Material> list = materialMapper.queryPage(startIndex, request.getLimit(), material);
            long count = materialMapper.queryPageCount(material);
            PageResult pageResult = new PageResult();
            pageResult.setRows(list);
            pageResult.setTotal(count);
            return pageResult;
        });
    }

    @Override
    public RestResult<Long> add(MaterialAddRequest request) {
        return executeQuery(() -> {
            Material material = new Material();
            BeanUtils.copyProperties(request, material);
            material.setIsDelete(false);
            material.setCreateTime(new Date());
            material.setUpdateTime(new Date());
            if(null != request.getAdminOperate()){
                material.setDeptId(request.getAdminOperate().getDeptId());
                material.setDeptType(request.getAdminOperate().getDeptType().getCode());
            }
//            material.setDeptType(DeptTypeEnums.CHILD_PLATFORM.getCode());
            long flag = materialMapper.insert(material);
            if(0 != flag){
                return material.getId();
            }
            return null;
        });
    }

    @Override
    public RestResult<Long> update(MaterialEditRequest request) {
        return executeQuery(() -> {
            AdminOperate operate = request.getAdminOperate();
            Material material = new Material();
            if (null != operate) {
                material.setUpdator(operate.getOperator());
                material.setUpdateName(operate.getOperateName());
            }
            material.setId(request.getId());
            material.setMaterialName(request.getMaterialName());
            material.setUpdateTime(new Date());

            long flag = materialMapper.update(material);
            return flag;
        });
    }

    @Override
    public RestResult<Long> delete(MaterialDeleteRequest request) {
        return executeQuery(() -> {
            AdminOperate operate = request.getAdminOperate();
            Material material = new Material();
            if (null != operate) {
                material.setUpdator(operate.getOperator());
                material.setUpdateName(operate.getOperateName());
            }
            material.setId(request.getId());
            material.setIsDelete(true);
            material.setUpdateTime(new Date());
            long flag = materialMapper.update(material);
            return flag;
        });
    }
}
